package vip.xumy.admin.um.controller;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.log4j.Log4j2;
import vip.xumy.admin.um.pojo.User;
import vip.xumy.admin.um.service.IUserService;
import vip.xumy.admin.utils.AdminLogUtil;
import vip.xumy.admin.utils.LoginUtil;
import vip.xumy.core.exception.CoreException;
import vip.xumy.core.pojo.base.BasePageParam;
import vip.xumy.core.pojo.com.AjaxResponse;
import vip.xumy.core.pojo.com.PageResponse;

/**	Ownership belongs to the company
 *	author:mengyxu
 *	date:2019年5月10日
 */

@Log4j2
@RestController
@SuppressWarnings("all")
@RequestMapping("admin/user")
public class UserController {
	@Autowired
	private IUserService userService;
	
	/**
	 * 用户管理条件查询 
	 * @param example 查询参数
	 * @return 分页查询数据
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public PageResponse<User> queryPageListByExample(User example, HttpServletRequest request){
		Page<User> pages = PageHelper.startPage(example.getPage(), example.getSize());
		List<User> list = userService.queryByExample(example);
		PageResponse<User> rsp = new PageResponse<>();
		rsp.setRows(list);
		rsp.setTotal(pages.getTotal());
		return rsp;
	}
	
	/**
	 * 状态管理分页查询 
	 * @param
	 * @return 分页查询数据
	 */
	@RequestMapping(value = "list/all", method = RequestMethod.GET)
	public PageResponse<User> queryAll(BasePageParam example, HttpServletRequest request){
		Page<User> pages = PageHelper.startPage(example.getPage(), example.getSize());
		List<User> list = userService.queryAll();
		PageResponse<User> rsp = new PageResponse<>();
		rsp.setRows(list);
		rsp.setTotal(pages.getTotal());
		return rsp;
	}
	
	/**
	 * 新增用户
	 * @param user 新增用户信息
	 * @return
	 * @throws CoreException 
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public AjaxResponse addUser(@RequestBody User user, HttpServletRequest request) throws CoreException{
		userService.insert(user);
		//记录、打印日志
		AdminLogUtil.saveUser(request, user);
		return new AjaxResponse(true, "新增用户成功！");
	}
	
	/**
	 * 修改用户其他信息
	 * @param user 用户信息
	 * @return
	 * @throws CoreException 
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public AjaxResponse updateUserInfo(@RequestBody User userInfo, HttpServletRequest request) throws CoreException{
		userService.updateInfo(userInfo);
		AdminLogUtil.updateUser(request, userInfo);
		return new AjaxResponse(true, "修改用户信息成功！");
	}
	
	/**
	 * 删除用户
	 * @param userId 待删除的用户名
	 * @return
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	public AjaxResponse removeUser(@RequestBody User user, HttpServletRequest request){
		String userId = user.getUserId();
		userService.deleteById(userId);
		AdminLogUtil.deleteUser(request, userId);
		return new AjaxResponse(true, "成功删除用户："+userId+"！");
	}
	
	/**
	 * 重置密码
	 * @param userName 待重置密码的用户名
	 * @return
	 */
	@RequestMapping(value = "reset", method = RequestMethod.POST)
	public AjaxResponse resetPassword(@RequestBody User user, HttpServletRequest request){
		String userId = user.getUserId();
		userService.resetKey(userId);
		AdminLogUtil.resetUserLog(request, userId);
		return new AjaxResponse(true, "成功重置用户："+userId+"的密码！");
	}
	
}
